package com.example.shopapp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.bean.Guanzhu;
import com.example.shopapp.bean.Luntan;
import com.example.shopapp.dao.GuanzhuDao;
import com.example.shopapp.database.CircleDbUtils;
import com.example.shopapp.database.GreenDaoManager;
import com.example.shopapp.database.PinglunDBUtils;
import com.example.shopapp.database.UserDBUtils;
import com.example.shopapp.widget.MyItemDecoration;
import com.kongzue.dialogx.dialogs.MessageDialog;

import java.util.List;



public class CirCleAdapter extends BaseQuickAdapter<Luntan, BaseViewHolder> {
    private Context context;
    private OnCircleListener listener;
    public CirCleAdapter(Context context, OnCircleListener listener) {
        super(R.layout.item_circle);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, Luntan luntan) {
        baseViewHolder.setText(R.id.tv_name, UserDBUtils.getInstance(context).getNameByUserId(luntan.getUser_id()));
        baseViewHolder.setText(R.id.tv_content,luntan.getContent());
        baseViewHolder.setText(R.id.tv_time,luntan.getTime());
        baseViewHolder.setText(R.id.tv_p, PinglunDBUtils.getInstance(context).getToTalByLuntanId(luntan.getId()+"")+"评论");
        baseViewHolder.setText(R.id.tv_z, getGuanzhu(luntan));
        Glide.with(context).load(R.mipmap.p_uncheck).into((ImageView) baseViewHolder.getView(R.id.img_pl));
        String path = UserDBUtils.getInstance(context).getHeadUrlBy(luntan.getUser_id());
        if (!TextUtils.isEmpty(path)){
            Glide.with(context).load(path).into((ImageView) baseViewHolder.getView(R.id.img_head));
        }
        List<String> stringList = CircleDbUtils.getInstance(context).findDiaryListByCircleId(luntan.getPic());
        if (stringList.size() == 0){
            baseViewHolder.getView(R.id.rl_img).setVisibility(View.GONE);
        }else {
            baseViewHolder.getView(R.id.rl_img).setVisibility(View.VISIBLE);
            CirCleImagListAdapter cirCleImagListAdapter = new CirCleImagListAdapter(context);
            RecyclerView recyclerView =  baseViewHolder.getView(R.id.rl_img);
            recyclerView.addItemDecoration(new MyItemDecoration(2));
            recyclerView.setLayoutManager(new GridLayoutManager(context,3));
            cirCleImagListAdapter.setNewData(stringList);
            recyclerView.setAdapter(cirCleImagListAdapter);
            cirCleImagListAdapter.notifyDataSetChanged();
        }

        baseViewHolder.getView(R.id.ll_p).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onCommentClick(luntan);
                }
            }
        });
        baseViewHolder.getView(R.id.ll_z).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onZanClick(luntan.getUser_id());
                }
            }
        });
        baseViewHolder.getView(R.id.ll_select).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onSelect(luntan.getId());
                }
            }
        });
    }

    private String getGuanzhu(Luntan luntan){
        GuanzhuDao guanzhuDao = GreenDaoManager.getmInstance().getDaoSession().getGuanzhuDao();
        List<Guanzhu> list = guanzhuDao.loadAll();
        if (list == null || list.size() ==0){

            return "关注";
        }
        for (int i = 0; i <list.size(); i++) {
            if (luntan.getUser_id().equals(list.get(i).getGuanzhu_id())
                    && App.getInstance().user.getUser_id().equals(list.get(i).getUser_id())){
                return "已经关注";
            }
        }

        return "关注";
    }

}
